package com.manage.modules.controller;

import java.util.List;
import java.util.Arrays;

import com.alibaba.fastjson.JSONObject;
import com.manage.modules.utils.DateUtil;
import com.manage.modules.utils.RandomUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.manage.common.annotation.Log;
import com.manage.common.core.controller.BaseController;
import com.manage.common.core.domain.AjaxResult;
import com.manage.common.enums.BusinessType;
import com.manage.modules.vo.TBaseRepairVo;
import com.manage.modules.bo.TBaseRepairQueryBo;
import com.manage.modules.bo.TBaseRepairAddBo;
import com.manage.modules.bo.TBaseRepairEditBo;
import com.manage.modules.service.ITBaseRepairService;
import com.manage.common.utils.poi.ExcelUtil;
import com.manage.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 维修单管理Controller
 *
 * @author wx
 * @date 2021-09-09
 */
@Api(value = "维修单管理控制器", tags = {"维修单管理管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/modules/repair")
public class TBaseRepairController extends BaseController {

    private final ITBaseRepairService iTBaseRepairService;

    /**
     * 获取维修单编号
     */
    @GetMapping("/getRepairCode")
    public AjaxResult getRepairCode() {
        JSONObject data = new JSONObject();
        data.put("repairCode", "repair_" + DateUtil.getNowDate() + RandomUtil.getRandomIntInRange(10000, 99999));
        return AjaxResult.success(data);
    }

    /**
     * 查询维修单管理列表
     */
    @ApiOperation("查询维修单管理列表")
    @PreAuthorize("@ss.hasPermi('modules:repair:list')")
    @GetMapping("/list")
    public TableDataInfo<TBaseRepairVo> list(TBaseRepairQueryBo bo) {
        startPage();
        List<TBaseRepairVo> list = iTBaseRepairService.queryList(bo);
        return getDataTable(list);
    }

    /**
     * 导出维修单管理列表
     */
    @ApiOperation("导出维修单管理列表")
    @PreAuthorize("@ss.hasPermi('modules:repair:export')")
    @Log(title = "维修单管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<TBaseRepairVo> export(TBaseRepairQueryBo bo) {
        List<TBaseRepairVo> list = iTBaseRepairService.queryList(bo);
        ExcelUtil<TBaseRepairVo> util = new ExcelUtil<TBaseRepairVo>(TBaseRepairVo.class);
        return util.exportExcel(list, "维修单管理");
    }

    /**
     * 获取维修单管理详细信息
     */
    @ApiOperation("获取维修单管理详细信息")
    @PreAuthorize("@ss.hasPermi('modules:repair:query')")
    @GetMapping("/{repairId}")
    public AjaxResult<TBaseRepairVo> getInfo(@PathVariable("repairId") Long repairId) {
        return AjaxResult.success(iTBaseRepairService.queryById(repairId));
    }

    /**
     * 新增维修单管理
     */
    @ApiOperation("新增维修单管理")
    @PreAuthorize("@ss.hasPermi('modules:repair:add')")
    @Log(title = "维修单管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody TBaseRepairAddBo bo) {
        return toAjax(iTBaseRepairService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改维修单管理
     */
    @ApiOperation("修改维修单管理")
    @PreAuthorize("@ss.hasPermi('modules:repair:edit')")
    @Log(title = "维修单管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody TBaseRepairEditBo bo) {
        return toAjax(iTBaseRepairService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除维修单管理
     */
    @ApiOperation("删除维修单管理")
    @PreAuthorize("@ss.hasPermi('modules:repair:remove')")
    @Log(title = "维修单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{repairIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] repairIds) {
        return toAjax(iTBaseRepairService.deleteWithValidByIds(Arrays.asList(repairIds), true) ? 1 : 0);
    }
}
